package com.x.ant.service;

import com.alibaba.fastjson.JSON;
import com.x.ant.dao.BillDao;
import com.x.ant.dao.DeptDao;
import com.x.ant.json.BillPrintResult;
import com.x.ant.manage.BillManage;
import com.x.ant.model.Bill;
import com.x.ant.model.BillTruck;
import com.x.ant.model.Dept;
import com.x.ant.model.Task;
import com.x.ant.util.BillPdfUtil;
import com.x.ant.util.PrintUtil;
import com.x.ant.util.SpringEmail;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

@Component
public class BillService extends BaseService {
    @Autowired
    private DeptDao deptDao;

    @Autowired
    private BillManage billManage;

    @Autowired
    private BillDao billDao;

    @Autowired
    private BillPdfUtil billPdfUtil;

    @Autowired
    private SpringEmail springEmail;

    public int selectBillNum(Bill bill) {
        try {
            return billDao.selectBillNum(bill);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Bill> selectBillList(Bill bill) {
        try {
            return billDao.selectBillList(bill);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

//    public void selectPrintBill(BillTruck billTruck, Task task, Dept dept, String billCode) {
//        try {
//            List<Dept> deptList = deptDao.selectDeptList(dept);
//            dept = deptList.get(0);
//            String pdfName = billCode + ".pdf";
//            File pdf = new File(this.getRealPath("download"), pdfName);
//            List<BillTruck> billTruckList = billDao.selectBillTruckList(billTruck);
//            pdf = billPdfUtil.createPDF(dept.getDeptName(), billCode, task, billTruckList, pdf);
//            springEmail.sendMailWithAttachment(dept.getDeptEmail(), billCode, billCode, pdf);
//        } catch (Exception e){
//            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
//        }
//    }

    public List<BillTruck> selectBillTruckList(BillTruck billTruck) {
        try {
            return billDao.selectBillTruckList(billTruck);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public BillTruck insertBillTruck(BillTruck billTruck, String truckIds) {
        try {
            return billManage.insertBillTruck(billTruck, truckIds);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void updateBill(Bill bill) {
        try {
            billManage.updateBill(bill);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void updateBillTruck(BillTruck billTruck) {
        try {
            billManage.updateBillTruck(billTruck);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteBillTruck(BillTruck billTruck) {
        try {
            billManage.deleteBillTruck(billTruck);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public BillPrintResult printBill(String printNum,
                          String deptPrintSn,
                          String deptName,
                          String deptAddress,
                          String taskSenderUserName,
                          String taskSenderUserMobile,
                          String taskLoadingArea,
                          String taskLoadingAddress,
                          String taskUnloadingArea,
                          String taskUnloadingAddress,
                          String taskGoodsStandard,
                          int taskFreightPrice,
                          String taskPayWay,
                          String taskRemark,
                          String truckUserName,
                          String truckMobile,
                          String truckNative,
                          int truckNum,
                          String truckPlates,
                          String billCode,
                          String staffUserMobile,
                          String url) {
        try {
            String result = PrintUtil.printBill(printNum,
                    deptPrintSn,
                    deptName,
                    deptAddress,
                    taskSenderUserName,
                    taskSenderUserMobile,
                    taskLoadingArea,
                    taskLoadingAddress,
                    taskUnloadingArea,
                    taskUnloadingAddress,
                    taskGoodsStandard,
                    taskFreightPrice,
                    taskPayWay,
                    taskRemark,
                    truckUserName,
                    truckMobile,
                    truckNative,
                    truckNum,
                    truckPlates,
                    billCode,
                    staffUserMobile,
                    url);
            BillPrintResult billPrintResult = JSON.parseObject(result, BillPrintResult.class);
            return billPrintResult;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

}
